require(['./config'], () => {
  require(['template', 'jquery', 'header', 'footer'], (template) => {
    class List {
      constructor () {
        this.Init().then(() => {
          this.listClick()
          this.setLocalStorage()
        })
      }

      Init () {
        return new Promise((resolve) => {
          $.get('http://rap2.taobao.org:38080/app/mock/264919/api/list', (resp) => {
            if (resp.code === "200") {
              const { data } = resp
              $('.listbook').eq(0).html(template('listbook-template', {
                data
              }))
              $('.listbook').eq(1).html(template('listbook-template', {
                data
              }))
            }

            
            
            

            let type = location.search.slice(14)
            let sex = location.search.slice(7,8)
            let url = null
            if ( type>8&&type<13 || type==14) {
              url = `https://app2.motie.com/ranking/detail?group=3&siteId=6&rankType=${type}&sex=${sex}&pageSize=30`
            } else if ( type == 13) {
              url = `https://app2.motie.com/ranking/detail?group=0&siteId=6&rankType=${type}&sex=${sex}&pageSize=30`
            } else if ( type == 17) {
              url = `https://app2.motie.com/ranking/detail?group=1&siteId=6&rankType=${type}&sex=${sex}&pageSize=30`
            }
            $.get(url, (resp) => {
              if (resp.error_no == 200) {
                
                console.log(resp)
                const { data } = resp
                this.data = data
                $('.booklist').html(template('booklist-template', {
                  data
                }))
              }
              resolve()
            })

            
            
          })
          
        }) 
      }

      listClick () {
        $('.listbook').eq(0).attr('data-count', location.search.slice(14))
        $('.listbook').eq(0).children().each((value, key) => {
          if ($(key).data('value') == location.search.slice(14) && location.search.slice(7,8)==1) {
            $(key).addClass('on')
          }
          $(key).on('click', function () {
            const type = $(this).data('value')
            location.assign(`/html/list.html?2&sex=1&type=${type}`)
          })
        })
        $('.listbook').eq(1).attr('data-count', location.search.slice(14))
        $('.listbook').eq(1).children().each((value, key) => {
          if ($(key).data('value') == location.search.slice(14) && location.search.slice(7,8)==2) {
            $(key).addClass('on')
          }
          $(key).on('click', function () {
            const type = $(this).data('value')
            location.assign(`/html/list.html?2&sex=2&type=${type}`)
          })
        })
        $('.listContent>.title>span').html($('.listbook').find('.on').html())
      }

      setLocalStorage () {
        const _this = this
        $('.addbtn').on('click', function (e) {
          e.preventDefault()
          console.log(_this.data)
          const data = _this.data.filter((item) => {
            return item.bookId == $(this).data('id')
          })
          console.log(data[0])
          const {
            bookName,
            introduce,
            bookId,
            bookIconUrl
          } = data[0]
          this.details = {
            bookName,
            content: introduce.toString().slice(0,35) + '...',
            bookId,
            price: bookId.toString().slice(0,2),
            img: bookIconUrl
          }
          console.log(this.details)
          let books = localStorage.getItem('books')
          if (books) {
            books = JSON.parse(books)
            const exist = books.some((book) => {
              return book.bookId === this.details.bookId
            })
            if (exist) {
              books = books.map((book) => {
                if (book.bookId === this.details.bookId) {
                  book.count++
                }
                return book
              })
            } else {
              books.push({
                ...this.details,
                count: 1,
                check: true
              })
            }
            localStorage.setItem('books', JSON.stringify(books))
          } else {
            localStorage.setItem('books', JSON.stringify([ {
              ...this.details,
              count: 1,
              check: true
            } ]))
          }

          
        })
      }

    }
    new List()
  })
})